package bll

import (
	"context"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type guideIcon struct {
	iGuideIcon store.IGuideIcon
}

var GuideIcon = &guideIcon{
	iGuideIcon: mysql.GuideIcon,
}

func init() {
	Register(GuideIcon)
}

func (a *guideIcon) init() func() {
	return func() {}
}

// Create
func (a *guideIcon) Create(ctx context.Context, in *model.GuideIconCreateRequest) error {
	var (
		err error
	)
	c := buildGuideIcon(in)
	_, err = a.iGuideIcon.Create(ctx, c)
	return err
}

// Update
func (a *guideIcon) Update(ctx context.Context, in *model.GuideIconUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.ConfigKey != nil {
		dict["config_key"] = in.ConfigKey
	}

	if in.Label != nil {
		dict["label"] = in.Label
	}

	if in.ConfigValue != nil {
		dict["config_value"] = in.ConfigValue
	}

	if in.Action != nil {
		dict["action"] = in.Action
	}

	if in.TagMenu != nil {
		dict["tag_menu"] = in.TagMenu
	}

	if in.SmallIcon != nil {
		dict["small_icon"] = in.SmallIcon
	}

	if in.BigIcon != nil {
		dict["big_icon"] = in.BigIcon
	}

	if in.Tag != nil {
		dict["tag"] = in.Tag
	}

	if in.Sort != nil {
		dict["sort"] = in.Sort
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iGuideIcon.Update(ctx, in.Id, dict)
}

// Delete
func (a *guideIcon) Delete(ctx context.Context, in *model.GuideIconDeleteRequest) error {
	return a.iGuideIcon.Delete(ctx, in.Id)
}

// List
func (a *guideIcon) List(ctx context.Context, in *model.GuideIconListRequest) (*model.GuideIconListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.GuideIcon
		out   = &model.GuideIconListResponse{}
	)

	if total, list, err = a.iGuideIcon.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.GuideIconsEntityToDto(list)

	return out, nil
}

// Find
func (a *guideIcon) Find(ctx context.Context, in *model.GuideIconInfoRequest) (*model.GuideIconInfo, error) {
	var (
		err  error
		data *entity.GuideIcon
		out  = &model.GuideIconInfo{}
	)

	if data, err = a.iGuideIcon.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.GuideIconEntityToDto(data)
	return out, nil
}

// buildGuideIcon build entity
func buildGuideIcon(in *model.GuideIconCreateRequest) *entity.GuideIcon {

	ety := &entity.GuideIcon{}

	ety.ConfigKey = in.ConfigKey

	ety.Label = in.Label

	ety.ConfigValue = in.ConfigValue

	ety.Action = in.Action

	ety.TagMenu = in.TagMenu

	ety.SmallIcon = in.SmallIcon

	ety.BigIcon = in.BigIcon

	ety.Tag = in.Tag

	ety.Sort = in.Sort

	ety.BanStatus = in.BanStatus

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
